<?php

namespace Liuhelong\LaravelAdmin\Wechat;
use EasyWeChat\Kernel\Support\File;
use Liuhelong\LaravelAdmin\Wechat\Models\WechatOffiaccountNew;

class Common
{
    public static function getFile($type,$key){
        $wechat = app('liuhelong_wechat_service');
        try{
            if($type=='text'){
                return $key;
            }
            if($type=='image' || $type=='voice'){
                $StreamResponse = $wechat->handle('material','get',[$key]);
                
                $suffix = File::getStreamExt($StreamResponse->getBody()->getContents());
                if(!is_file(public_path("uploads/wechat_material/$key$suffix"))){
                    $StreamResponse->saveAs(public_path("uploads/wechat_material"),$key);
                }
                
                if($type=='image'){
                    return "<img src='/uploads/wechat_material/$key$suffix' style='width:200px'/>";
                }
                
                if($type=='voice'){
                    
                    return "<video controls='' src='/uploads/wechat_material/$key$suffix' style='width:300px;height:50px'>
                    您的浏览器不支持 video 标签。
                    </video>";
                }
            }
            if($type=='video'){ 
            
                $result = $wechat->handle('material','get',[$key]);
                return $result['title'];
            }
            
            if($type=='news'){ 
            
                return WechatOffiaccountNew::find($key)->title;
            }
            if($type=='mpnews'){ 
                $result = $wechat->handle('material','get',[$key]);
                return self::showNews($result);
            }
        }catch(\Exception $e){
            Log::error($e);
        }
    }
    
    public static function showNews($content){
        
        $str = "<div style='width:300px;background:#fff;color:#666666;border:1px solid #e2e2e2;border-radius:10px;overflow:hidden'>";
        
        $items = $content['news_item'];
        //info($items);
        
        $thumb = $items[0]['thumb_url'];
        $title = $items[0]['title'];
        $digest = $items[0]['digest'];
        
        if(count($items)==1){
            $str .="
            <div><img src='$thumb' style='width:300px;height:160px;'/></div>
                <div style='margin:0.5rem 1rem;'>
                    <h5 style='color:#000;font-size:15px;line-height:1.4'>$title</h5>
                    <div>$digest</div>
                </div>
            </div>";
        }else{
            $str .="
            <div style='position:relative;'><img src='$thumb' style='width:300px;height:160px;'/>
                <div style='position:absolute;width:100%;bottom:0;color:#fff;background:rgba(0,0,0,0.3)'>
                    <h5 style='padding:0 1rem;font-size:15px;line-height:1.4'>$title</h5>
                </div>
            </div>";
            
            for($i=1;$i<count($items);$i++){
                $thumb = $items[$i]['thumb_url'];
                $title = $items[$i]['title'];
                $digest = $items[$i]['digest'];
                $str .="
                <div style='margin:1rem;display:flex;'>
                    <div style='flex: 1 1 auto!important;white-space: normal!important;word-break: break-word!important;word-wrap: break-word!important;'>
                        $title
                    </div>";
                if($thumb)
                    $str .="<img src='$thumb' style='width:65px;height:65px;'/>";
                    
                $str .="</div>";
                
                if($i!=count($items)-1){
                    $str .="<hr style='margin:1rem;display:flex;'>";
                }
            }
            
            $str .="</div>";
        }
        
        return $str;
    }
}